﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Caching;
using System.IO;
using System.Collections;

namespace Tools
{
    /// <summary>
    /// Summary description for CachingHelper
    /// </summary>

    public class CachingHelper
    {
        public CachingHelper()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        //public static string CacheChudu24ProductionCommunity
        //{
        //    get { return "CacheChudu24ProductionCommunity"; }
        //}

        public static object GetCacheObject(string cacheKey)
        {
            System.Web.Caching.Cache cache = System.Web.HttpRuntime.Cache;
            object cacheObject = cache.Get(cacheKey);
            return cacheObject;
        }

        public static bool SetCacheObject(string cacheKey, object cacheItem)
        {
            try
            {
                string dependencyFile = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["CacheDependencyFolder"] + "/" + cacheKey + ".txt");
                if (!File.Exists(dependencyFile))
                    File.Create(dependencyFile);
                System.Web.Caching.Cache cache = System.Web.HttpRuntime.Cache;
                CacheDependency cacheDependency = new CacheDependency(dependencyFile);
                cache.Insert(cacheKey, cacheItem, cacheDependency);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static bool SetNoCacheDependencyObject(string cacheKey, object cacheobject, int Minutes)
        {
            try
            {
                System.Web.Caching.Cache cache = System.Web.HttpRuntime.Cache;
                cache.Insert(cacheKey, cacheobject, null, DateTime.Now.AddMinutes(Minutes), Cache.NoSlidingExpiration);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        public static bool SetSqlCacheDataTable(string cacheKey, DataTable cacheDataTable, System.Data.SqlClient.SqlCommand sqlCommand)
        {
            try
            {
                SqlCacheDependency cacheDependency = new SqlCacheDependency(sqlCommand);
                System.Web.Caching.Cache cache = System.Web.HttpRuntime.Cache;
                cache.Insert(cacheKey, cacheDataTable, cacheDependency);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public static bool ExpireParticularCache(string cacheKey)
        {
            try
            {
                string dependencyFile = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["CacheDependencyFolder"] + "/" + cacheKey + ".txt");
                Random r = new Random();
                File.WriteAllText(dependencyFile, r.Next().ToString());
            }

            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public static bool CreateCacheSql(string cacheKey, object cacheItem, string tableName, string cacheDatabaseName)
        {
            try
            {
                SqlCacheDependency sqlCacheDependen = new SqlCacheDependency(cacheDatabaseName, tableName);
                HttpContext.Current.Cache.Insert(cacheKey, cacheItem, sqlCacheDependen, DateTime.Now.AddDays(1), Cache.NoSlidingExpiration);
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public static object GetCacheSql(string cacheKey)
        {
            return HttpContext.Current.Cache[cacheKey] as object;
        }
        public static void RemoveAllCache()
        {
            IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();

            while (enumerator.MoveNext())
            {
                if (enumerator.Key.ToString().Contains("CheckRolesActions_"))
                {
                    HttpContext.Current.Cache.Remove(enumerator.Key.ToString());
                }
            }
        }
    }
}